package zdm.core;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @Author: 张德明
 * @CreateTime: 2025-06-17 17:14:00
 * @Description: bean工厂，用于存储实例化完的bean
 */

public class BeanFactory {
    private static Map<String, Object> beans = new ConcurrentHashMap<>();

    // 添加一个bean
    public static void addBean(String beanName, Object bean) {
        // 先判断是否已经存在该bean
        if (beans.containsKey(beanName)) {
            System.out.println("Bean " + beanName + "已经存在，将要被覆盖");
        }
        beans.put(beanName, bean);
        System.out.println("Bean " + beanName + "加入成功");
    }

    // 根据名称获取一个bean
    public static Object getBean(String beanName) {
        return beans.get(beanName);
    }


    // 获取所有的bean,用于扫描自动注入
    public static Map<String, Object> getAllBeans() {
        return beans;
    }

    // 清空所有的bean
    public static void clearBeans() {
        beans.clear();
        System.out.println("所有的Bean已被清空");
    }


    public static Set<String> getAllBeanNames() {
        return beans.keySet();
    }
}
